const app = getApp()
var t = getApp().api,
  a = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid:"",
    access_token:"",
    object:"",
    imgon_off:false,
    show:false,
    add:true,
    add_posts:1,
    posts_info: {
      content:'',
      title:'',
      pic_list:[],
      video_list: [],
      video_pic_list:[],
      video_pic_list_url: [],
      address:{},
      address_show: 1,
      pic_list_url:[],
      video_list_url: [],
      topic:{},
      goods:{},
      longitude: 0,
      latitude: 0
    },
  },
  jianyan:function(e){
    // wx.request({
    //   url: 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN',
    //   data:{
    //     content
    //   }
    // })
  },
   //跳转页面
   goUrl: function (e) {
    var gopage = e.currentTarget.dataset.url;
    if (!gopage) {
      return;
    }
    getApp().core.navigateTo({
      url: gopage
    });
  },
  loadData: function (t) {
    var a = this;
    getApp().core.showLoading({
      title: "加载中"
    }), getApp().request({
      url: getApp().api.weitao.article_detail,
      data: {
        article_id: t.article_id
      },
      success: function (t) {
        0 == t.code && a.setData({
          article: t.data.detail
        }), 1 == t.code && (getApp().core.showModal({
          title: "提示",
          content: t.msg,
          showCancel: !1,
          success: function (t) {
            if (t.confirm) {
              wx.navigateBack({
                delta: 1, // 返回上一级页面。
                success: function () {
                  console.log('成功！')
                }
              })
            }
            console.log(t.confirm);
          }
        }));
        
    // a.data.article.content && wx.setNavigationBarTitle({
    //   title: a.data.article.content.substring(0,5)
    // })

      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (t) {
    var that= this
  //   getApp().core.getStorage({
  //     key: "USER_INFO",
  //     success: function(a) {
  //       console.log(a,'获取凭证')
  //         that.setData({
  //           access_token:a.data.access_token
  //         })
  //     }
  // })
    console.log(this,'onload')
    wx.login({
      success:function(res){
        console.log(res,'code')
        var code=res.code
        wx.request({
        url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + 'wx0c087b956a8ce22b' + '&secret=' + 'ab3c7256ebee3b3c296a5c8e0c89cb0a' + '&js_code=' + code + '&grant_type=authorization_code',
         success:function(res){
           console.log(res.data.openid,'openid')
           that.setData({
             openid:res.data.openid
           })
         }
        })
      }
    })
    wx.request({
      url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET',
      data:{
        grant_type:'client_credential',
        appid:'wx0c087b956a8ce22b',
        secret:'ab3c7256ebee3b3c296a5c8e0c89cb0a'
      },
      success:function(res){
        console.log(res,'获取凭证')
        that.setData({
          access_token:res.data.access_token
        })
      }
    })
    var that=this;
    wx.setNavigationBarTitle({
      title: '发帖'
    })
    getApp().page.onLoad(this, t);
    this.getData(t),this.getLocation();
    getApp().request({
      url: getApp().api.weitao.article_detail,
      data: {
        article_id: t.articleId
      },
      success:function(t){
        console.log(t.data.detail)
        that.setData({
          object:t.data.detail
        })
      }
    });
  },
  getLocation(){
    var t = this;
    wx.getLocation({
    success: function (a) {
      t.setData({
        longitude: a.longitude,
        latitude: a.latitude
      });
      t.locationContent();
    },
    complete: function () {
    }
  });
},
  locationContent: function () {
    var o = this;
    getApp().request({
      url: getApp().api.mch.location_info,
      method: "GET",
      data: {
        longitude: o.data.longitude,
        latitude: o.data.latitude
      },
      success: function (t) {
      if(t.code==0){
        o.data.posts_info.address = {
          name: t.data.address.name,
          address: t.data.address.name,
          latitude:  t.data.address.location.lat,
          longitude:  t.data.address.location.lng
        }
        o.setData({
          posts_info: o.data.posts_info
        })
      }else{
        o.setData({
          myAddress: "定位失败1",
        })
      }
        
      },
      fail: function (t) {
        o.setData({
          myAddress: "定位失败2",
        })
      },
    });
  },
  getData(t){
    var a = this;
    getApp().core.showLoading({
        title: "加载中"
    }), getApp().request({
        url: getApp().api.weitao.add_article_view,
        data: {
          topic_id: t.topic_id,
          goods_id: t.goods_id
        },
        success: function(t) {
            0 == t.code && (a.data.posts_info.topic = t.data.topic,a.data.posts_info.goods = t.data.goods, a.setData({
              posts_info: a.data.posts_info
            }));
        },
        complete: function() {
            getApp().core.hideLoading();
        }
    });
  },
  dingwei: function () {
    return false;
    var t = this;
    wx.chooseLocation({
      success: function (e) {
        t.data.posts_info.address = e
        console.log(e);
        t.setData({
          posts_info: t.data.posts_info
        })
      },
      fail: function (a) {
        a.getauth({
          content: "需要获取您的地理位置授权，请到小程序设置中打开授权",
          success: function (e) {
            e && (e.authSetting["scope.userLocation"] ? t.dingwei() : wx.showToast({
              title: "您取消了授权",
              image: "/images/icon-warning.png"
            }));
          }
        });
      },
      complete: function () {
        
      }
    });
  },
  switchAddress(t){
    var o = this;
    o.data.posts_info.address_show = t.detail.value ? 1 : 0;
    this.setData({
      posts_info: o.data.posts_info
    })
  },
  deleteTopic(){
    var s = this;
    delete s.data.posts_info.topic;
    s.setData({
      posts_info: s.data.posts_info
    });

  },
  contentInput(t){
    var o = this;
    wx.request({
      method:'POST',
      url: 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN',
      data:{
        access_token:o.data.access_token,
        content:o.data.posts_info.content,
        version:2,
        openid:o.data.openid,
        scene:1,
      },
      success:function(res){
        console.log(res,'内容检验')
      }
    })
    o.data.posts_info.content = t.detail.value;
    this.setData({
      posts_info: o.data.posts_info
    });
    console.log(o.data,'内容');
  },
  contentInputtitle(t){
    console.log(t,'标题')
    
    var o = this;
    wx.request({
      method:'POST',
      url: 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN',
      data:{
        access_token:o.data.access_token,
        content:o.data.posts_info.title,
        version:2,
        openid:o.data.openid,
        scene:1,
      },
      success:function(res){
        console.log(res,'标题检验')
      }
    })
    o.data.posts_info.title = t.detail.value;
    this.setData({
      posts_info: o.data.posts_info
    });
    console.log(o.data,'标题2');
    
  },
  click_img() {
    this.setData({
      imgon_off:true
    })
  },
  add_img() {
    wx.showActionSheet({
      itemList: ['从手机相册选择', '拍照'],
      success: function (res) {
        console.log(res.tapIndex)
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  // 文件类型
  add_posts: function () {
    var that = this;
    this.setData({
      add_posts: !that.data.add_posts
    })
  },
  chooseVideo: function (t) {
    var e = this,
      i = e.data.posts_info,
      o = i.video_list.length;
      if (o >= 1 || i.pic_list.length>0){
          wx.showToast({
            icon: 'none',
            title: '只能上传一个视频，或九张图片'
          })
          return;
        }
        wx.chooseVideo({
          count: 1,
          success: function (t) {
            console.log(t);
            e.add_posts()
            i.video_pic_list = i.video_pic_list.concat(t.thumbTempFilePath),i.video_list = i.video_list.concat(t.tempFilePath), e.setData({
              posts_info: i
            });
          }
        });
  },
  chooseImage: function(t) {
    var e = this,
      i = e.data.posts_info,
      o = i.pic_list.length,
      v = i.video_list.length;
      if (o >= 9 || v >= 1) {
        wx.showToast({
          icon: 'none',
          title: '只能上传一个视频，或九张图片'
        })
        return;
      }
    wx.chooseImage({
      count: 9 - o,
      success: function(t) {
        e.add_posts()
        i.pic_list = i.pic_list.concat(t.tempFilePaths), e.setData({
          posts_info: i
        });
      }
    });
  },
  deleteImage: function(t) {
    console.log(t);
    var e = this,
      i = t.currentTarget.dataset.picIndex,
      o = e.data.posts_info;
    o.pic_list.splice(i, 1), e.setData({
      posts_info: o
    });
  }, 

  deleteVideo: function(t) {
    var e = this,
      i = t.currentTarget.dataset.picIndex,
      o = e.data.posts_info;
      o.video_list.splice(i, 1), o.video_pic_list.splice(i, 1), e.setData({
      posts_info: o
    });
  }, 
  release: function (a) {
    //上传图片
    function i(e) {
      if (e == n.length) return o();
      var ii =0;
      for (var s in n) !function (oo) {
        wx.uploadFile({
          url: t.default.upload_image,
          name: "image",
          formData: r,
          filePath: n[oo],
          complete: function (t) {
            if (t.data) {
              var s = JSON.parse(t.data);
              if (0 == s.code && (posts_info.pic_list_url[oo] = s.data.url)){
                that.setData({
                  posts_info: posts_info
                })
                ii++;
              }
            }
            if (ii==n.length) return o();
          }
        });
      }(s);
    }
    // 上传视频缩略图
    function video_pic(e) {
      if (e == vid_pic.length) return vv(0);
      var ii = 0;
      for (var s in vid_pic) !function (oo) {
        if (!vid_pic[oo]) return vv(0);
        wx.uploadFile({
          url: t.default.upload_image,
          name: "image",
          formData: r,
          filePath: vid_pic[oo],
          complete: function (t) {
            if (t.data) {
              var s = JSON.parse(t.data);
              if (0 == s.code && (posts_info.video_pic_list_url[oo] = s.data.url)) {
                that.setData({
                  posts_info: posts_info
                })
                ii++;
              }
            }
            if (ii == vid_pic.length) return vv(0);
          }
        });
      }(s);
    }


    // 上传视频
    function vv(e) {
      if (e == vid.length) return o();
      var ii = 0;
      for (var s in vid) !function (oo) {
        wx.uploadFile({
          url: t.default.upload_video,
          name: "video",
          formData: r,
          filePath: vid[oo],
          complete: function (tt) {
            if (tt.data) {
              var ss = JSON.parse(tt.data);
              if (0 == ss.code && (posts_info.video_list_url[oo] = ss.data.url)) {
                that.setData({
                  posts_info: posts_info
                })
                ii++;
              }
            }
            if (ii == vid.length) return o();
          }
        });
      }(s);
    }

    function o() {
      var post_data = {
        title:posts_info.title,
        content: posts_info.content,
        address_show: posts_info.address_show ? posts_info.address_show : 0,
        video_list_url: posts_info.video_list.length > 0 ? JSON.stringify(posts_info.video_list_url) : '',
        pic_list_url: JSON.stringify(posts_info.pic_list_url),
        topic_id: posts_info.topic.id,
        goods_id: posts_info.goods ? posts_info.goods.id : 0,
        address_latitude: posts_info.address.latitude ? posts_info.address.latitude : '',
        address_longitude: posts_info.address.longitude ? posts_info.address.longitude : '',
      };
      getApp().request({
        url: t.weitao.add_article,
        method: "post",
        data: post_data,
        success: function (t) {
          wx.hideLoading(), wx.showModal({
            title: "提示",
            content: t.msg,
            showCancel: !1,
            success: function (e) {
              e.confirm && t.code == 0 && wx.navigateTo({
                url: "/weitao/index/index"
              })
            }
          })
        }
      });
    }
    var s = this;
    var that = this;
    var n = s.data.posts_info.pic_list, vid = s.data.posts_info.video_list, vid_pic = s.data.posts_info.video_pic_list ,posts_info = s.data.posts_info, r = {};
    if (posts_info.content == "" && posts_info.pic_list.length < 1 && posts_info.video_list.length < 1) {
      wx.showModal({
        title: "提示",
        content: "请填写内容或上传图片或视频",
        showCancel: !1,
        success: function (e) {
          
        }
      })
      return false;
    } 
    // if (!posts_info.goods) {
    //   wx.showModal({
    //     title: "提示",
    //     content: "请选择商品",
    //     showCancel: !1,
    //     success: function (e) {

    //     }
    //   })
    //   return false;
    // }
    if (!posts_info.topic) {
      wx.showModal({
        title: "提示",
        content: "请选择话题",
        showCancel: !1,
        success: function (e) {

        }
      })
      return false;
    }
    wx.showLoading({
      title: "正在提交",
      mask: !0
    });
    if(vid.length > 0){
      video_pic(0)
    }else{
      i(0);
    }
    
  },
  getBackData(){

  },
  //跳转页面
  goUrl: function (e) {
    var gopage = e.currentTarget.dataset.url,s=this;
    if (!gopage) {
      return;
    }
    wx.navigateTo({
      url: gopage,
      events: {
        someEvent: function(data) {
          console.log(data)
          s.getData(data);
        }
      },
      success: function(res) {
      }
    })
  },
})